# -*- coding: utf-8 -*-
# @Time : 2024/5/1 9:26 AM 
# @Author : Wayne
# @Email : 1
# @File : t_jindie.py
# @Project : young_python_demo
# @DESCRIPTION :
import json

from erp.erp import api_sdk
from wms import wms, model as wms_model
from tools import dbmysql
import traceback
import time
import datetime
from datetime import datetime,timedelta
import hashlib
import requests
import pandas as pd
import random

# 物料调取库存同步
def goodsKucunTongbu(goodsNumber):

        print("goodsNumber")

        res2 = api_sdk.ExecuteBillQuery(
                    {
                        "FormId": "STK_Inventory",
                        "FieldKeys": "FBaseQty",
                        "FilterString": "FMaterialId.FNumber = '" + goodsNumber + "' and  FStockId.FNumber = '51' ",
                        "OrderString": "FMaterialId.FNumber",
                        "TopRowCount": 0,
                        "StartRow": 0
                    })
        sal_outstock_list2 = json.loads(res2)

        ll = len(sal_outstock_list2)

        l = 0

        if ll >0:

          for s in sal_outstock_list2:
            FBaseQty = int(s[0])
            l = l + FBaseQty

        print(l)

        return l


def wuliaoxiafaoldwebmin(count):
 print("新物料下发至老网站开始运行")
 try:
    # time1 = datetime.now()
    # t = (time1 + timedelta(seconds=-7200)).strftime("%Y-%m-%d %H:%M:%S")

    # FilterString = "FForbidDate >='" + t + "' or FModifyDate  >= '" + t + "' or FCreateDate >= '" + t + "'"

    # FilterString = "FNumber = 'c029332'"
    FilterString = ""

    num = count * 2000
    print(num)

    res = api_sdk.ExecuteBillQuery(
        {
            "FormId": "BD_MATERIAL",
            "FieldKeys": "FMATERIALID,FNumber,FName,F_UDHS_GGXH.FDataValue,F_UDHS_CDPP.FNumber,FStoreUnitID.Fname,F_UDHS_BZS,F_UDHS_Text,F_UDHS_SPFLSJ.Fnumber,F_UDHS_BM.FDataValue,F_UDHS_CDPP.FDataValue, F_UDHS_TYM.FDataValue,F_UDHS_ZJMDH,F_UDHS_TJCX,FForbidStatus,F_UDHS_CheckBox_zhibao,F_UDHS_Yzd,F_UDHS_GYSWLBM,F_UDHS_WEB_delete,FIsSale",
            "FilterString": FilterString,
            "OrderString": "FCreateDate",
            "TopRowCount": 0,
            "StartRow": num
        })
    sal_outstock_list = json.loads(res)
    #print(sal_outstock_list)
    l = len(sal_outstock_list)
    # print(l)
    if l == 2000:

        for i in sal_outstock_list:

          FForbidStatus = i[14]  # 是否禁用
          zhibao = i[15]  # 是否易制爆
          F_UDHS_SFWZDWP = i[16]  # 是否易制爆
          F_UDHS_WEB_delete = i[18] #危化品
          FIsSale = i[19]  # 允许销售
          FNumber = i[1]

          if FForbidStatus == 'A' and zhibao == False and F_UDHS_SFWZDWP != '1' and FIsSale != False:

            is_delete = '0'

            Fid = i[0]  # 为与原物料区分开
            FNumber = i[1]  # 物料编码
            FName = i[2].strip()  # 物料名称
            FGGXH = i[3]  # 规格
            FCDPPNumber = i[4]  # 产地品牌编码
            FStoreUnitID = i[5]  # 库存单位
            F_UDHS_BZS = i[6]  # 大包装
            F_UDHS_Text = i[7]  # 中包装

            F_UDHS_SPFLSJ = i[8]  # 分类三
            if FCDPPNumber == '9623':

                if FNumber == 'sxsdjp384' or FNumber == 'sxsdj1764' or FNumber == 'sxsdjs645' or FNumber == 'szddly1164' or FNumber == 'bxsrjy2' or FNumber == 'szddly1165' or FNumber == 'lzj4734':
                    F_UDHS_SPFLSJ = '5011024'  # 分类三
                else:
                    F_UDHS_SPFLSJ = '5011025'  # 分类三

            F_UDHS_BM = i[9].strip() if i[9] else ""  # 别名
            FCDPPName = i[10].strip() if i[10] else ""  # 产地品牌
            F_UDHS_GYSWLBM = ''
            F_UDHS_GYSWLBM = i[17]  # 供应商物料编码

            keywords = ""
            if i[11] != None:
              F_UDHS_TYMFDataValue = i[11].strip()  # 通用名
            else:
              F_UDHS_TYMFDataValue = ''

            if i[12] != None:
              F_UDHS_ZJMDH = i[12].strip()  # 助记码
            else:
              F_UDHS_ZJMDH = ''

            F_UDHS_TJCX = i[13]  #特价促销

            if F_UDHS_TJCX == True:
                tejia = '1'
            else:
                tejia = '0'

            #print(is_delete)

            keywords = FCDPPName + '、' + F_UDHS_BM + '、' + F_UDHS_TYMFDataValue  # 关键词
            #print(FNumber)

            if FCDPPNumber != None:

                SQLBrandID = dbmysql.searchBrand(FCDPPNumber)

                if SQLBrandID == 'none':
                    dbmysql.insertBrand(FCDPPName, FCDPPNumber)
                    # print(FCDPPNumber)
                else:
                    dbmysql.updateBrand(FCDPPName, SQLBrandID, FCDPPNumber)
                    #print('已有产地品牌')

            else:
                SQLBrandID = ''

            sQlName = dbmysql.searchGoods(FNumber)

            if sQlName == 'none':
                print("新增了一个")
                print(FNumber)
                Fid2 = str(Fid) + '0'
                res2 = api_sdk.ExecuteBillQuery(
                    {
                        "FormId": "BD_SAL_PriceList",
                        "FieldKeys": "FNumber,FPrice",
                        "FilterString": "FMaterialId.FNumber = '" + FNumber + "' and FEntryForbidStatus = 'A'",
                        "OrderString": "",
                        "TopRowCount": 0,
                        "StartRow": 0,
                        "Limit": 0,
                        "SubSystemId": ""
                    })
                jiage = json.loads(res2)
                # print(jiage)
                shopPrice = '0'
                market_price = '0'
                if FCDPPNumber == '9686' or FCDPPNumber == '10119' or FCDPPNumber == '10616' or FCDPPNumber == '10487' or FCDPPNumber == '10089' or FCDPPNumber == '9593' or FCDPPNumber == 'FZ117172' or FCDPPNumber == 'FZ116894' or FCDPPNumber == '10484' or FCDPPNumber == 'FZ9325' or FCDPPNumber == 'FZ13470' or FCDPPNumber == 'FZ13469' or FCDPPNumber == 'FZ1154' or FCDPPNumber == 'FZ1480' or FCDPPNumber == '80004' or FCDPPNumber == '9698':
                    shopPrice = 'none'
                    market_price = 'none'
                    dbmysql.delPrice(sQlGoodsID)
                else:
                  for p in jiage:
                    z = p[0]
                    price = p[1]
                    num = ''

                    if z != '':
                        print('有价格')
                        if z == 'LSJMB':
                                num = '5'
                                shopPrice = p[1]
                                market_price = float(shopPrice)*1.2
                        if z == 'WFJMB':
                                num = '1'
                        if z == 'XSJMB0003':
                                num = '3'
                        if z == 'EPWFJMB':
                                num = '2'
                        if z == 'EPSNJMB':
                                num = '4'

                        dbmysql.Goodsjiage(Fid2, num, price)

                goods_number = goodsKucunTongbu(FNumber)

                dbmysql.synchronizationGoods(Fid2, FNumber, FName, SQLBrandID, F_UDHS_ZJMDH, FGGXH, FStoreUnitID,
                                             F_UDHS_Text, F_UDHS_BZS, keywords, F_UDHS_TYMFDataValue,
                                             tejia, is_delete,market_price,shopPrice,F_UDHS_GYSWLBM,goods_number)
            else:

                sQlGoodsID = dbmysql.searchGoodsID(FNumber)

                res2 = api_sdk.ExecuteBillQuery(
                    {
                        "FormId": "BD_SAL_PriceList",
                        "FieldKeys": "FNumber,FPrice",
                        "FilterString": "FMaterialId.FNumber = '" + FNumber + "' and FEntryForbidStatus = 'A'",
                        "OrderString": "",
                        "TopRowCount": 0,
                        "StartRow": 0,
                        "Limit": 0,
                        "SubSystemId": ""
                    })
                jiage = json.loads(res2)
                shopPrice = '0'
                market_price = '0'
                ll = len(jiage)
                if FCDPPNumber == '9686' or FCDPPNumber == '10119' or FCDPPNumber == '10616' or FCDPPNumber == '10487' or FCDPPNumber == '10089' or FCDPPNumber == '9593' or FCDPPNumber == 'FZ117172' or FCDPPNumber == 'FZ116894' or FCDPPNumber == '10484' or FCDPPNumber == 'FZ9325' or FCDPPNumber == 'FZ13470' or FCDPPNumber == 'FZ13469' or FCDPPNumber == 'FZ1154' or FCDPPNumber == 'FZ1480' or FCDPPNumber == '80004' or FCDPPNumber == '9698':
                    shopPrice = 'none'
                    print(shopPrice)
                    dbmysql.delPrice(sQlGoodsID)
                  # print(ll)

                else:
                  if ll > 0:
                    ishasPrice = dbmysql.searchPrice(sQlGoodsID)
                    for p in jiage:
                        # print(p)
                        z = p[0]
                        price = p[1]
                        num = ''
                        if z != '':
                            if z == 'LSJMB':
                                num = '5'
                                shopPrice = p[1]
                                market_price = float(shopPrice)*1.2
                            if z == 'WFJMB':
                                num = '1'
                            if z == 'XSJMB0003':
                                num = '3'
                            if z == 'EPWFJMB':
                                num = '2'
                            if z == 'EPSNJMB':
                                num = '4'
                            if ishasPrice != 'none':
                                dbmysql.jiageSQL(sQlGoodsID, num, price)
                            else:
                                dbmysql.Goodsjiage(sQlGoodsID, num, price)

                  else:
                    #print("运行删除价格")
                    dbmysql.delPrice(sQlGoodsID)

                goods_number = goodsKucunTongbu(FNumber)

                # print("现有库存")
                # print(goods_number)

                dbmysql.updateGoods(FNumber, FName, SQLBrandID, F_UDHS_ZJMDH, FGGXH, FStoreUnitID, F_UDHS_Text,
                                    F_UDHS_BZS,
                                    keywords, F_UDHS_TYMFDataValue, tejia, is_delete,market_price,shopPrice,F_UDHS_GYSWLBM,goods_number)

          else:
             print("此为禁用物料或制度至爆")
             print(FNumber)
             FNumber = i[1]  # 物料编码

             dbmysql.updateGoodsDelete(FNumber)

        count = count + 1
        wuliaoxiafaoldwebmin(count)

    else:

        for i in sal_outstock_list:
            FForbidStatus = i[14]  # 是否禁用
            zhibao = i[15]  # 是否易制爆
            F_UDHS_SFWZDWP = i[16]  # 是否易制爆
            F_UDHS_WEB_delete = i[18]  # 危化品
            FIsSale = i[19]  # 允许销售
            FNumber = i[1]

            if FForbidStatus == 'A' and zhibao == False and F_UDHS_SFWZDWP != '1' and FIsSale != False:
                is_delete = '0'

                Fid = i[0]  # 为与原物料区分开
                FNumber = i[1]  # 物料编码

                FName = i[2].strip()  # 物料名称
                FGGXH = i[3]  # 规格
                FCDPPNumber = i[4]  # 产地品牌编码
                FStoreUnitID = i[5]  # 库存单位
                F_UDHS_BZS = i[6]  # 大包装
                F_UDHS_Text = i[7]  # 中包装

                print(FCDPPNumber)

                # print(F_UDHS_BZS)
                # print(F_UDHS_Text)
                F_UDHS_SPFLSJ = i[8]  # 分类三
                if FCDPPNumber == '9623':

                    if FNumber == 'sxsdjp384' or FNumber == 'sxsdj1764' or FNumber == 'sxsdjs645' or FNumber == 'szddly1164' or FNumber == 'bxsrjy2' or FNumber == 'szddly1165' or FNumber == 'lzj4734':
                        F_UDHS_SPFLSJ = '5011024'  # 分类三
                    else:
                        F_UDHS_SPFLSJ = '5011025'  # 分类三
                F_UDHS_BM = i[9].strip() if i[9] else ""  # 别名
                FCDPPName = i[10].strip() if i[10] else ""  # 产地品牌

                F_UDHS_GYSWLBM = i[17]  # 供应商物料编码

                if F_UDHS_GYSWLBM:
                    F_UDHS_GYSWLBM = i[17]
                else :
                    F_UDHS_GYSWLBM = ''

                # if F_UDHS_GYSWLBM != '':
                #     FName = "[" + F_UDHS_GYSWLBM + "] " + FName
                # else :
                #     F_UDHS_GYSWLBM = ''

                keywords = ""
                if i[11] != None:
                    F_UDHS_TYMFDataValue = i[11].strip()  # 通用名
                else:
                    F_UDHS_TYMFDataValue = ''

                if i[12] != None:
                    F_UDHS_ZJMDH = i[12].strip()  # 助记码
                else:
                    F_UDHS_ZJMDH = ''

                F_UDHS_TJCX = i[13]  # 特价促销

                if F_UDHS_TJCX == True:
                    tejia = '1'
                else:
                    tejia = '0'

                # print(is_delete)

                keywords = FCDPPName + '、' + F_UDHS_BM + '、' + F_UDHS_TYMFDataValue  # 关键词
                print(FNumber)

                if FCDPPNumber != None:

                    SQLBrandID = dbmysql.searchBrand(FCDPPNumber)

                    if SQLBrandID == 'none':
                        dbmysql.insertBrand(FCDPPName, FCDPPNumber)
                        # print(FCDPPNumber)
                    else:
                        dbmysql.updateBrand(FCDPPName, SQLBrandID,FCDPPNumber)
                        print('已有产地品牌')

                else:
                    SQLBrandID = ''

                sQlName = dbmysql.searchGoods(FNumber)

                print(sQlName)

                if sQlName == 'none':
                    print("新增了一个")
                    print(FNumber)

                    Fid2 = str(Fid) + '0'
                    res2 = api_sdk.ExecuteBillQuery(
                        {
                            "FormId": "BD_SAL_PriceList",
                            "FieldKeys": "FNumber,FPrice",
                            "FilterString": "FMaterialId.FNumber = '" + FNumber + "' and FEntryForbidStatus = 'A'",
                            "OrderString": "",
                            "TopRowCount": 0,
                            "StartRow": 0,
                            "Limit": 0,
                            "SubSystemId": ""
                        })
                    jiage = json.loads(res2)
                    # print(jiage)
                    shopPrice = '0'
                    market_price = '0'
                    if FCDPPNumber == '9686' or FCDPPNumber == '10119' or FCDPPNumber == '10616' or FCDPPNumber == '10487' or FCDPPNumber == '10089' or FCDPPNumber == '9593' or FCDPPNumber == 'FZ117172' or FCDPPNumber == 'FZ116894' or FCDPPNumber == '10484' or FCDPPNumber == 'FZ9325' or FCDPPNumber == 'FZ13470' or FCDPPNumber == 'FZ13469' or FCDPPNumber == 'FZ1154' or FCDPPNumber == 'FZ1480' or FCDPPNumber == '80004' or FCDPPNumber == '9698':
                        shopPrice = 'none'
                    else:
                      for p in jiage:

                        z = p[0]
                        price = p[1]
                        num = ''
                        if z != '':
                            if z == 'LSJMB':
                                num = '5'
                                shopPrice = p[1]
                                market_price = float(shopPrice) * 1.2

                            if z == 'WFJMB':
                                num = '1'
                            if z == 'XSJMB0003':
                                num = '3'
                            if z == 'EPWFJMB':
                                num = '2'
                            if z == 'EPSNJMB':
                                num = '4'

                            dbmysql.Goodsjiage(Fid2, num, price)


                    goods_number = goodsKucunTongbu(FNumber)

                    print(F_UDHS_GYSWLBM)

                    dbmysql.synchronizationGoods(Fid2, FNumber, FName, SQLBrandID, F_UDHS_ZJMDH, FGGXH, FStoreUnitID,
                                                 F_UDHS_Text,
                                                 F_UDHS_BZS, keywords, F_UDHS_TYMFDataValue,
                                                 tejia, is_delete,market_price,shopPrice,F_UDHS_GYSWLBM, goods_number)

                else:

                    sQlGoodsID = dbmysql.searchGoodsID(FNumber)

                    res2 = api_sdk.ExecuteBillQuery(
                        {
                            "FormId": "BD_SAL_PriceList",
                            "FieldKeys": "FNumber,FPrice",
                            "FilterString": "FMaterialId.FNumber = '" + FNumber + "' and FEntryForbidStatus = 'A'",
                            "OrderString": "",
                            "TopRowCount": 0,
                            "StartRow": 0,
                            "Limit": 0,
                            "SubSystemId": ""
                        })
                    jiage = json.loads(res2)
                    shopPrice = '0'
                    market_price = '0'
                    ll = len(jiage)
                    #print("ll")
                    if FCDPPNumber == '9686' or FCDPPNumber == '10119' or FCDPPNumber == '10616' or FCDPPNumber == '10487' or FCDPPNumber == '10089' or FCDPPNumber == '9593' or FCDPPNumber == 'FZ117172' or FCDPPNumber == 'FZ116894' or FCDPPNumber == '10484' or FCDPPNumber == 'FZ9325' or FCDPPNumber == 'FZ13470' or FCDPPNumber == 'FZ13469' or FCDPPNumber == 'FZ1154' or FCDPPNumber == 'FZ1480' or FCDPPNumber == '80004' or FCDPPNumber == '9698':
                        shopPrice = 'none'
                        dbmysql.delPrice(sQlGoodsID)
                        print(shopPrice)
                    else:
                      if ll > 0:
                        ishasPrice = dbmysql.searchPrice(sQlGoodsID)
                        for p in jiage:
                            # print(p)
                            z = p[0]
                            price = p[1]
                            num = ''
                            if z != '':

                                    if z == 'LSJMB':
                                        num = '5'
                                        shopPrice = p[1]
                                        market_price = float(shopPrice)*1.2
                                    if z == 'WFJMB':
                                        num = '1'
                                    if z == 'XSJMB0003':
                                        num = '3'
                                    if z == 'EPWFJMB':
                                        num = '2'
                                    if z == 'EPSNJMB':
                                        num = '4'
                                    if ishasPrice != 'none':
                                        dbmysql.jiageSQL(sQlGoodsID, num, price)
                                    else:
                                        dbmysql.Goodsjiage(sQlGoodsID, num, price)

                      else:
                        #print("运行删除价格")
                        dbmysql.delPrice(sQlGoodsID)

                    goods_number = goodsKucunTongbu(FNumber)

                    # print("现有库存")
                    # print(goods_number)
                    #print(FCDPPNumber)


                    dbmysql.updateGoods(FNumber, FName, SQLBrandID, F_UDHS_ZJMDH, FGGXH, FStoreUnitID, F_UDHS_Text,
                                        F_UDHS_BZS,
                                        keywords, F_UDHS_TYMFDataValue, tejia, is_delete,market_price,shopPrice,F_UDHS_GYSWLBM,goods_number)

            else:
                print("此为禁用物料")
                FNumber = i[1]  # 物料编码
                time.sleep(0.5)
                print(FNumber)
                dbmysql.updateGoodsDelete(FNumber)
        #maikelin()
        print('一天的物料同步完了')

        #kelin()
        return

 except Exception as e:
        time.sleep(10)
        count = count
        wuliaoxiafaoldwebmin(count)

        return